/*
 * @ProjectName: flutter_common
 * @Package: 
 * @ClassName: login_password.dart
 * @Description: 输入密码页面
 * @User: Page.Su
 * @Date: 2020/8/24
 * @Time: 17:22
 * @UpdateDate: 2020/8/24 17:22
 */

import 'dart:async';

import 'package:fdottedline/fdottedline.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_su/common/application.dart';
import 'package:flutter_su/common/language_text.dart';
import 'package:flutter_su/common/app_color.dart';
import 'package:flutter_su/common/fun.dart';
import 'package:flutter_su/router/login_register_router.dart';

class LoginPasswordPage extends StatefulWidget {
  @override
  _LoginPasswordPageState createState() => _LoginPasswordPageState();
}

class _LoginPasswordPageState extends State<LoginPasswordPage> {
  // 提示密码是否有误
  StreamController<bool> _showPasswordErrorController =
      StreamController<bool>();

  // 显示输入框底部虚线
  StreamController<bool> _hiddenTextFileLine =
      StreamController<bool>.broadcast();

  // 输入就控制器
  TextEditingController _passwordInputController = TextEditingController();

  bool _isEmail = false;

  @override
  void dispose() {
    _showPasswordErrorController.close();
    _hiddenTextFileLine.close();
    _passwordInputController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: LanguageText.loginText,
        centerTitle: true,
        elevation: 0,
        backgroundColor: AppColor.MAIN_COLOR_BLUE,
      ),
      backgroundColor: AppColor.MAIN_COLOR_BLUE,
      body: _body(),
    );
  }

  Widget _body() {
    var bodyWidget = Container(
      padding: EdgeInsets.only(
        top: SFunCommon.height(340),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Padding(
            child: LanguageText.inputPassword,
            padding: EdgeInsets.only(left: SFunCommon.width(36)),
          ),
          _emailTextFileBox(),
          _emailTipText(),
          _nextButton(),
          _forgetPassword(),
        ],
      ),
    );

    return GestureDetector(
      onTap: () {
        FocusScope.of(context).requestFocus(FocusNode());
      },
      child: SingleChildScrollView(
        child: bodyWidget,
      ),
    );
  }

  // 邮箱没有注册时的提示文字
  Widget _emailTipText() {
    return StreamBuilder(
      initialData: false,
      stream: _showPasswordErrorController.stream,
      builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
        return Padding(
          padding: EdgeInsets.only(
            left: SFunCommon.width(36),
          ),
          child: Offstage(
            offstage: !snapshot.data,
            child: LanguageText.passwordError,
          ),
        );
      },
    );
  }

  Widget _emailTextFileBox() {
    return Container(
      padding: EdgeInsets.only(
        left: SFunCommon.width(36),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          _emailTextFile(),
          _line(),
        ],
      ),
    );
  }

  Widget _emailTextFile() {
    return Container(
      width: SFunCommon.width(600),
      height: SFunCommon.height(50),
      child: TextField(
        controller: _passwordInputController,
        autofocus: false,
        obscureText: true,
        keyboardType: TextInputType.emailAddress,
        style: SFunCommon.fontStyle(
          AppColor.MAIN_INPUT_COLOR,
          fontSize: 36,
        ),
        onChanged: (String value) {
          _showPasswordErrorController.sink.add(false);
          if (value.length == 0) {
            _hiddenTextFileLine.sink.add(false);
          } else {
            _hiddenTextFileLine.sink.add(true);
            // 邮件进行正则表达式校验
            if (value.length < 6) {
              _isEmail = false;
            } else {
              _isEmail = true;
            }
          }
        },
        cursorColor: AppColor.MAIN_INPUT_COLOR,
        decoration: InputDecoration(
          // 未获得焦点下划线设为灰色
          enabledBorder: UnderlineInputBorder(
            borderSide: BorderSide(color: AppColor.MAIN_COLOR_BLUE),
          ),
          //获得焦点下划线设为蓝色
          focusedBorder: UnderlineInputBorder(
            borderSide: BorderSide(color: AppColor.MAIN_COLOR_BLUE),
          ),
        ),
      ),
    );
  }

  // 下划线
  Widget _line() {
    return StreamBuilder(
      initialData: false,
      stream: _hiddenTextFileLine.stream,
      builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
        return Offstage(
          offstage: snapshot.data,
          child: FDottedLine(
            color: Colors.white,
            width: SFunCommon.width(336),
            strokeWidth: 2.0,
            dottedLength: 10.0,
            space: 2.0,
          ),
        );
      },
    );
  }

  // 下一步的按钮
  Widget _nextButton() {
    var streamWidget = StreamBuilder(
      initialData: true,
      stream: _hiddenTextFileLine.stream,
      builder: (BuildContext context, _) {
        return RaisedButton(
          child: LanguageText.goToBitBro,
          color: AppColor.MAIN_COLOR_CYAN,
          disabledColor: AppColor.MAIN_COLOR_CYAN_DISABLE,
          padding: EdgeInsets.only(
            left: SFunCommon.width(239),
            right: SFunCommon.width(239),
            top: SFunCommon.height(23),
            bottom: SFunCommon.height(23),
          ),
          onPressed: _isEmail ? _nextFun : null,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(
              SFunCommon.width(20),
            ),
          ),
        );
      },
    );

    return Container(
      alignment: Alignment.center,
      margin: EdgeInsets.only(
        top: SFunCommon.height(506),
      ),
      child: streamWidget,
    );
  }

  // 忘记密码
  Widget _forgetPassword() {
    Widget widget = GestureDetector(
      onTap: () {
        Application.router.navigateTo(
          context,
          LoginRegisterRouter.FORGET_PASSWORD,
          transition: TransitionType.nativeModal,
        );
      },
      child: Container(
        alignment: Alignment.centerRight,
        margin: EdgeInsets.only(
          right: SFunCommon.width(91),
          top: SFunCommon.height(30),
        ),
        child: LanguageText.forgetPassword,
      ),
    );

    return widget;
  }

  void _nextFun() {
    String text = _passwordInputController.text;
  }
}
